package com.googlecode.grt192.HH11.sensor;

import java.awt.Color;
import java.awt.Graphics;

import com.googlecode.grtframework.event.SwitchEvent;
import com.googlecode.grtframework.event.SwitchListener;
import com.googlecode.grtframework.sensor.ISwitch;
import com.googlecode.grtframework.vis.Displayable;
import com.googlecode.grtframework.vis.IMountedPosition;
import com.googlecode.grtframework.vis.Mountable;

/**
 * SwitchDisplay draws the state of a switch
 * 
 * @author ajc
 * 
 */
public class SwitchDisplay implements Displayable, SwitchListener, Mountable {

	private static final int DISPLAY_WIDTH = 32;
	private static final int DISPLAY_HEIGHT = 32;
	private static final int CLICK_WIDTH = 25;
	private static final int CLICK_HEIGHT = 25;

	private final IMountedPosition pos;

	private boolean state;
	private final ISwitch s;
	private final String name;

	public SwitchDisplay(ISwitch s, IMountedPosition pos, String name) {
		this.s = s;
		this.pos = pos;
		this.name = name;
	}

	@Override
	public void switchPressed(SwitchEvent sw) {
		state = true;
	}

	@Override
	public void switchReleased(SwitchEvent sw) {
		state = false;
	}

	@Override
	public void startListening() {
		s.addSwitchListener(this);
	}

	@Override
	public void stopListening() {
		s.removeSwitchListener(this);
	}

	// *********
	// MOUNTABLE
	// *********
	@Override
	public double getHeading() {
		return pos.getHeading();
	}

	@Override
	public int getX() {
		return pos.getX();
	}

	@Override
	public int getY() {
		return pos.getY();
	}

	@Override
	public void display(Graphics g) {
		// gray frame
		g.setColor(Color.gray);
		g.fillRect(getX() - DISPLAY_WIDTH / 2, getY() - DISPLAY_HEIGHT / 2,
				DISPLAY_WIDTH, DISPLAY_HEIGHT);

		// center clicky thing
		if (state) {// press
			g.setColor(Color.blue);
		} else {// release
			g.setColor(Color.DARK_GRAY);
		}
		g.fillRect(getX() - CLICK_WIDTH / 2, getY() - CLICK_HEIGHT / 2,
				CLICK_WIDTH, CLICK_HEIGHT);

		g.setColor(Color.BLACK);
		g.drawString(name, getX(), getY() - DISPLAY_HEIGHT / 2);
	}

}
